#!/bin/sh
#
# see: dh_installdeb(1)

set -e

#http://software-dl.ti.com/codegen/non-esd/downloads/download.htm#C6000
package="ti-c6000-cgt-v8.0.x-installer"

#ce47b4e95e31fddd69183c8b671ebf90f3ef40e3eadfc62fd9200f24406e9e51  ti_cgt_c6000_8.0.1_armlinuxa8hf_busybox_installer.sh
#f435f886610fc7df585a6ef898494e621b3bfa17f20658436f5e63af86a9a987  ti_cgt_c6000_8.0.3_armlinuxa8hf_busybox_installer.sh
#e8c3251d8a54e06ffc980dd8f9204abe0b21d68f5cdd7d0efeb69a78b3b7f77c  ti_cgt_c6000_8.0.4_armlinuxa8hf_busybox_installer.sh

version="8.0.4"
FILENAME="ti_cgt_c6000_${version}_armlinuxa8hf_busybox_installer.sh"
SHA256SUM_TGZ="e8c3251d8a54e06ffc980dd8f9204abe0b21d68f5cdd7d0efeb69a78b3b7f77c"
PARTNER_URL="http://downloads.ti.com/codegen/esd/cgt_public_sw/C6000/${version}"

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

dl_file() {
	wget --no-check-certificate $PARTNER_URL/$FILENAME
}

validdate_file() {
	sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
	if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
		rm -rf $FILENAME
		dl_file
		sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
		if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
			echo "dl failure"
			exit 1
		fi
	fi
}

dl_install() {
	if [ ! -d /var/cache/${package} ] ; then
		mkdir -p /var/cache/${package}
	fi

	cd /var/cache/${package}/

	if [ ! -f $FILENAME ] ; then
		dl_file
	fi

	if [ -f $FILENAME ] ; then
		validdate_file

		chmod +x $FILENAME
		./$FILENAME --prefix / --temp `mktemp -d`
	fi
}

case "$1" in
    configure)
	dl_install
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

